package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.BalanceDao;
import com.mstore.pojo.Balance;

public class BalanceService {

	private BalanceDao balanceDao;

	public void addBalance(Balance balance) {
		balanceDao.addBalance(balance);
	}

	public Balance selectById(Balance balance) {
		Balance c = (Balance) balanceDao.selectById(balance);
		c.setCreateTime(c.getCreateTime().substring(0,
				c.getCreateTime().indexOf(".")));
		return c;
	}

	/**
	 * 查询店铺的充值记录
	 */
	public List<Balance> getBalancePage(int index, Balance balance) {
		List<Object> objects = balanceDao.getBalancePage(index, balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	/**
	 * 根据时间查询
	 */
	public List<Balance> getBalancePageByTime(int index, Balance balance,
			String beginTime, String endTime) {
		List<Object> objects = balanceDao.getBalancePageByTime(index, balance,
				beginTime, endTime);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	/**
	 * 根据shop和account来查询总页数
	 * 
	 * @param index
	 * @param balance
	 * @return
	 */
	public int getBalancePageTotal(Balance balance, String beginTime,
			String endTime) {
		List<Object> objcets = balanceDao.getBalancePageByTimeTotal(balance,
				beginTime, endTime);
		return objcets.size();
	}

	/**
	 * 查询企业全部充值记录
	 */
	public List<Balance> getEnterpriseBalancePage(int index, Balance balance) {
		List<Object> objects = balanceDao.getEnterpriseBalancePage(index,
				balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	/**
	 * 获取店铺的充值条数
	 */
	public int getBalanceLine(Balance balance) {
		return balanceDao.getBalanceLine(balance);
	}

	/**
	 * * 获取企业的充值条数 /** * @return
	 */
	public int getEnterpriseBalanceLine(Balance balance) {
		return balanceDao.getEnterpriseBalanceLine(balance);
	}

	/**
	 * 获取会员在某店铺的充值条数
	 */
	public int getOneBalanceLine(Balance balance) {
		return balanceDao.getOneBalanceLine(balance);
	}

	/**
	 * 获取会员充值条数
	 */
	public int getEnterpriseOneBalanceLine(Balance balance) {
		return balanceDao.getEnterpriseOneBalanceLine(balance);
	}

	/**
	 * 分页查询一个会员在某家店铺的充值记录
	 */
	public List<Balance> getBalancePageByMobileAndShop(int index,
			Balance balance) {
		List<Object> objects = balanceDao.getBalancePageByMobileAndShop(index,
				balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	public List<Balance> getBalancesBymember(Balance balance) {
		List<Object> objects = balanceDao.getBalancesBymember(balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance blce = (Balance) object;
			blce.setCreateTime(blce.getCreateTime().substring(0,
					blce.getCreateTime().indexOf(".")));
			balances.add(blce);
		}
		return balances;
	}

	/**
	 * 分页查询一个会员的充值记录
	 */
	public List<Balance> getBalancePageByMoble(int index, Balance balance) {
		List<Object> objects = balanceDao.getBalancePageByMoble(index, balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	/**
	 * 获取店铺全部充值记录
	 */
	public List<Balance> getBalances(Balance balance) {
		List<Object> objects = balanceDao.getBalances(balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	/**
	 * 获取企业全部充值记录
	 */
	public List<Balance> getEnterpriseBalances(Balance balance) {
		List<Object> objects = balanceDao.getEnterpriseBalances(balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	public List<Balance> getBalancesByshop(Balance balance) {
		List<Object> objects = balanceDao.getBalancesByshop(balance);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance ba = (Balance) object;
			ba.setCreateTime(ba.getCreateTime().substring(0,
					ba.getCreateTime().indexOf(".")));
			balances.add(ba);
		}
		return balances;
	}

	public int getByCreateTimeAndMemberIdPage(Balance balance,
			String beginTime, String endTime) {
		return balanceDao.getByCreateTimeAndMemberIdPage(balance, beginTime,
				endTime);

	}

	public List<Balance> getByCreateTimeAndMemberId(int index, Balance balance,
			String beginTime, String endTime) {
		List<Object> objects = balanceDao.getByCreateTimeAndMemberId(index,
				balance, beginTime, endTime);
		List<Balance> balances = new ArrayList<Balance>();
		for (Object object : objects) {
			Balance co = (Balance) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			balances.add(co);
		}
		return balances;
	}

	/**
	 * 根据时间段查询
	 */
	public List<Balance> getBalancePageConsumeByTime(Balance balance,
			String beginTime, String endTime) {
		List<Balance> balances = new ArrayList<Balance>();
		List<Object> objects = balanceDao.getBalancePageConsumeByTime(balance,
				beginTime, endTime);
		for (Object object : objects) {
			Balance co = (Balance) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			balances.add(co);
		}
		return balances;
	}

	/**
	 * 查询在某企业的充值记录中的会员
	 */
	public List<Long> findMemberIdFromBalance(Long eaId) {
		return balanceDao.findMemberIdFromBalance(eaId);
	}

	/**
	 * 通过eaid和memberid 查询会员充值记录
	 * 
	 * @param balance
	 * @return
	 */
	public List<Balance> getBalanceByEaIdAndMemberId(Balance balance) {
		List<Balance> balances = new ArrayList<Balance>();
		List<Object> objects = balanceDao.getBalanceByEaIdAndMemberId(balance);
		for (Object object : objects) {
			Balance co = (Balance) object;
			// 日期的转换
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			balances.add(co);
		}
		return balances;
	}

	public void deleteById(Balance balance) {
		balanceDao.deleteById(balance);
	}

	/**
	 * 查询会员在某企业的充值总数
	 */
	public Double getEabalance(Balance balance) {
		Object object = balanceDao.getEabalance(balance);
		if (object != null) {
			return (Double) object;
		}
		return 0d;
	}

	/**
	 * 查询某时间段内在企业中充值金额总量
	 */
	public Double getSumBalanceByTime(Long eaId, String begin, String end) {
		return balanceDao.getSumBalanceByTime(eaId, begin, end);
	}

	/**
	 * 查询某时间段内在分店中消费金额总量
	 */
	public Double getSumBalanceByTimeAndShopId(Long shopId, String begin,
			String end) {
		return balanceDao.getSumBalanceByTimeAndShopId(shopId, begin, end);
	}
	
	/**
	 * 根据搜索条件查询充值记录
	 * 分页
	 */
	public List<Balance> getBalancesByRequirePage(int index, Balance balance) {
		List<Balance> balances = new ArrayList<Balance>();
		List<Object> objects = balanceDao.getBalancesByRequirePage(index, balance);
		for (Object object : objects) {
			balances.add((Balance) object);
		}
		return balances;
	}
	
	/**
	 * 根据搜索条件查询充值记录，取得记录总数
	 */
	public int getBalancesByRequireCount(Balance balance) {
		return balanceDao.getBalancesByRequireCount(balance);
	}
	
	/**
	 * 根据搜索条件查询充值记录
	 */
	public List<Balance> getBalancesByRequire(Balance balance) {
		List<Balance> balances = new ArrayList<Balance>();
		List<Object> objects = balanceDao.getBalancesByRequire(balance);
		for (Object object : objects) {
			balances.add((Balance) object);
		}
		return balances;
	}

	public BalanceDao getBalanceDao() {
		return balanceDao;
	}

	public void setBalanceDao(BalanceDao balanceDao) {
		this.balanceDao = balanceDao;
	}
}
